import axios from 'axios'
import Vue from 'vue'
import router from './router'
const http = axios.create({
    baseURL:process.env.VUE_APP_API_URL||'/'
    // baseURL: 'http://localhost:3030/',
})

http.interceptors.request.use(function (config) {
    // Do something before request is sent
    if (sessionStorage.token) {
        config.headers.Authorization = 'Bearer ' + (sessionStorage.token || '')
    }
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});


http.interceptors.response.use(res => {
    return res
}, err => {
    if (err.response.data.msg) {
        Vue.prototype.$message({
            message: err.response.data.msg
        })
    }
    if (err.response.status == 412) {
        router.push('/login')
    }

    return Promise.reject(err)
})
export default http;